package com.crack.smartgit;

import java.io.*;
import java.util.HashMap;

public final class VersionMap {
    public final static String ENTRY = "MAIN";
    public final static String METHOD = "METHOD";
    public final static String PARMTYPELIST = "PARMTYPELIST";


    public final static HashMap<String,String> V18_2_5 = new HashMap<String,String>();

    static {
        V18_2_5.put(ENTRY, "smartgit/alV");
        V18_2_5.put(METHOD, "a");
        V18_2_5.put(PARMTYPELIST, "ajA,MessageDigest");
    }

    public final static HashMap<String,String> V18_2_6 = new HashMap<String,String>();

    static {
        V18_2_6.put(ENTRY, "smartgit/alW");
        V18_2_6.put(METHOD, "a");
        V18_2_6.put(PARMTYPELIST, "ajB,MessageDigest");
    }

    public final static HashMap<String,HashMap> Versions = new HashMap<String,HashMap>();

    static {
        Versions.put("18.2.5", V18_2_5);
        Versions.put("18.2.6", V18_2_6);
    }

    private final static String CONFIG = "target.txt";
    public   static String CurrentVersion="18.2.5";

    public static HashMap GetTargetVersion(){
        File f=new File(CONFIG);
        HashMap result=null;
        if(!f.exists()){
            try {
                LogHelper.Debug("The configuration file does not exist and the configuration file will be automatically created and written to the supported default version 18.2.5");
                result=Versions.get(CurrentVersion);
                f.createNewFile();
                FileWriter fileWriter= new FileWriter(f);
                fileWriter.write(CurrentVersion);
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
                LogHelper.Error("Write configuration failed",e);
            }
        }
        else{
            try {
                BufferedReader reader = new BufferedReader(new FileReader(f));
                CurrentVersion= reader.readLine().trim();
                result=Versions.getOrDefault(CurrentVersion,null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                LogHelper.Error("configuration file not found",e);
            } catch (IOException e) {
                e.printStackTrace();
                LogHelper.Error("Write configuration failed",e);
            }
        }
        return result;
    }
}
